package com.zx.scp.controller;

import com.zx.platform.utils.ImageScale;
import com.zx.platform.utils.KeyUtils;
import com.zx.scp.service.model.ImageSettleRequestModel;
import com.zx.scp.service.model.ImageSettleResponse;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 图片处理服务器
 *
 * @author jefferry
 */
@Controller
@RequestMapping("/imageSettle")
public class ImageSettleController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(ImageSettleController.class);
    /**
     * 系统图片上传地址
     */
    public static final String SYSTIMESAVEPATH = "/uploadFile";
    /**
     * 系统app图片上传格式
     */
    public static final String SYSUPLOADIMGTYPE = ".JPG";

    @RequestMapping("/save")
    @ResponseBody
    public ImageSettleResponse send(ImageSettleRequestModel imageSettleRequestModel,
                                    Model model, HttpServletRequest request) {

        ImageSettleResponse imageSettleResponse = new ImageSettleResponse();
        try {//图片保存处理
            String realPath = request.getSession().getServletContext()
                    .getRealPath(SYSTIMESAVEPATH)+ File.separator + imageSettleRequestModel.getFileKey();
            File pathFile = new File(realPath);
            if (!pathFile.exists()) {
                // 文件夹不存 创建文件
                pathFile.mkdirs();
            }
            String radomFileName = KeyUtils.getDateStrAndRandom();
            String fileName = radomFileName + SYSUPLOADIMGTYPE;
            String tempFilePath = realPath +File.separator+ "temp"
                    + File.separator + fileName;
            if (imageSettleRequestModel.getImgInfo() == null) // 图像数据为空
                return null;
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                // Base64解码
                byte[] bytes = decoder.decodeBuffer(imageSettleRequestModel.getImgInfo());
                for (int i = 0; i < bytes.length; ++i) {
                    if (bytes[i] < 0) {// 调整异常数据
                        bytes[i] += 256;
                    }
                }
                // 生成jpeg图片
                File tempFile = new File(tempFilePath);
                if (!tempFile.exists()) {
                    tempFile.getParentFile().mkdirs();
                }
                // 将文件copy上传到服务器
                FileUtils.writeByteArrayToFile(tempFile, bytes);
                ImageScale.resizeFix(tempFile, new File(realPath + File.separator
                        + fileName), 384, 512); // 提供2种规则图片格式
                String maxFileName = radomFileName + "_1"
                        + SYSUPLOADIMGTYPE;
                ImageScale.resizeFix(tempFile, new File(realPath + File.separator
                        + maxFileName), 768, 1024);
                imageSettleResponse.setReturnCode("0000");
                imageSettleResponse.setImageKey(fileName);
            } catch (Exception e) {
                log.error("app上传图片处理异常", e);
                imageSettleResponse.setReturnCode("9999");
                imageSettleResponse.setErrMsg("app上传图片处理异常");
            }
        } catch (Exception e) {
            log.error("app上传图片处理异常", e);
            imageSettleResponse.setReturnCode("9999");
            imageSettleResponse.setErrMsg("app上传图片处理异常");
        }
        return imageSettleResponse;
    }

}
